package com.uyaer.wheel.display.scene
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.common.UserData;
	import com.uyaer.wheel.display.widget.Hit;
	import com.uyaer.wheel.display.widget.HitPool;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.game.Game;
	import com.uyaer.wheel.game.map.MapCellType;
	import com.uyaer.wheel.res.ResManager;
	import com.uyaer.wheel.texture.TexturePool;
	import com.uyaer.wheel.tick.TimerManager;
	
	import flash.geom.Point;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import starling.utils.VAlign;
	
	/**
	 *游戏积分等信息 
	 * @author hc
	 * 
	 */	
	public class GameInfo extends Sprite implements IScene
	{
	
		private var starIcon:MovieClip;
		private var starCountTF:TextField;
		private var lvTF:TextField;
		
		private const MAX_HIT_TIME:Number = 3000; //最大击打时间
		private var hitTime:Number = 0; //剩余连击时间 
		private var hitCount:int = 0; //连击次数
		private var w:Number;
		private var isRuning:Boolean = true;
		private var hitProgress:Quad;
		private var hit:Hit;
		private var levelUp:Image;
		
		public function GameInfo()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded(e:Event = null):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			
			w = Math.min(400,Common.W*0.7);
			
			this.layout();
		}
		
		
		/**
		 *布局 
		 */		
		private function layout():void{
			
			starCountTF = new TextField(200,60,UserData.me.starCount+"",Common.FONT,48,0xff8800,true);
			starCountTF.vAlign = VAlign.TOP;
			starCountTF.hAlign = HAlign.LEFT;
			starCountTF.x = 48;
			starCountTF.y = 1;
			addChild(starCountTF);
			
			var lvImg:Image = new Image(ResManager.me.getTexture("menu_gamelv"));
			lvImg.x = Common.W-70-lvImg.width;
			lvImg.y = 10;
			addChild(lvImg);
			
			lvTF = new TextField(60,40,UserData.me.level+"",Common.LVFONT,38,0x00ee22);
			lvTF.hAlign = HAlign.LEFT;
			lvTF.pivotX = 60;
			lvTF.x = Common.W;
			lvTF.y = 4;
			addChild(lvTF);
			
			hitProgress = new Quad(w,24,0xff9900);
			hitProgress.x = (Common.W-w)*0.5;
			hitProgress.y = Common.H * 0.8;
			
			levelUp = new Image(ResManager.me.getTexture("menu_levelup"));
			levelUp.pivotX = levelUp.width>>1;
			levelUp.x = Common.W>>1;
			levelUp.scaleY = levelUp.scaleX = Common.S;
		}
		
		/**
		 *添加一个星星 
		 * @param e
		 * 
		 */		
		private function eatSatrHandler(e:Event):void{
			hitCount++;
			UserData.me.starCount += hitCount;
			var pos:Point = e.data as Point;
			var star:MovieClip = new MovieClip(ResManager.me.getTextures("map_star"),6);
			star.x = pos.x;
			star.y = pos.y;
			addChild(star);
			Starling.juggler.add(star);
			var d:Number = Point.distance(new Point(starIcon.x,starIcon.y),pos);
			var t:Number = Math.ceil(d / 100) * 0.1 + 0.3;
			Starling.juggler.tween(star,t,{"x":starIcon.x,"y":starIcon.y,
							"transition":Transitions.EASE_OUT,
							"onComplete":_starAniOver,
							"onCompleteArgs":[star]});
			
			//显示连击进度条
			this.showHitProgress();
			//显示hit信息
			if(hitCount>=3){
				if(hit){
					hit.removeFromParent();
				}
				hit = HitPool.getHit();
				hit.updateHit(hitCount);
				addChild(hit);
				var sx:Number = pos.x + 50;
				var sy:Number = pos.y + 50;
				if(sx>Common.W-hit.width)sx-=100;
				if(sy>Common.H-hit.height)sy-=100;
				hit.show(sx,sy);
			}
		}
		
		/**
		 *显示连击进度条 
		 */		
		private function showHitProgress():void{
			hitTime = MAX_HIT_TIME;
			if(hitCount==1){
				addChild(hitProgress);
				TimerManager.addLoop(refreshProgress,MAX_HIT_TIME/20);
			}
		}
		
		private function refreshProgress():void{
			if(!isRuning)return;
			hitTime-=MAX_HIT_TIME/20;
			if(hitTime<=0){
				hitTime = 0;
				hitCount = 0;
				TimerManager.removeLoop(refreshProgress);
				removeChild(hitProgress);
			}
			hitProgress.width = hitTime/MAX_HIT_TIME * w;
		}
		
		private function _starAniOver(star:MovieClip):void{
			Starling.juggler.remove(star);
			TexturePool.recycle(MapCellType.STAR,star);
			starCountTF.text = UserData.me.starCount+"";
		}
		
		public function start():void{
			isRuning = true;
			starIcon = new MovieClip(ResManager.me.getTextures("map_star"),6);
			starIcon.x = 8;
			starIcon.y = 8;
			addChild(starIcon);
			Starling.juggler.add(starIcon);
			starCountTF.text = UserData.me.starCount+"";
			lvTF.text = UserData.me.level+"";
			stage.addEventListener(EventType.EAT_STAR,eatSatrHandler);
			stage.addEventListener(EventType.LEVEL_UP,leveUpHandler);
			stage.addEventListener(EventType.GAME_STATE_CHANGE,onGameStageChangeHandler);
		}
		
		private function onGameStageChangeHandler(e:Event):void{
			var state:String = e.data as String;
			if(state == Game.PAUSE){
				isRuning = false;
				Starling.juggler.remove(starIcon);
			}else if(state == Game.RUNNING){
				isRuning = true;
				Starling.juggler.add(starIcon);
			}
		}
		
		private function leveUpHandler():void{
			lvTF.text = UserData.me.level+"";
			levelUp.y = Common.H;
			addChild(levelUp);
			Starling.juggler.tween(levelUp,1,{"y":Common.H*0.45,"transition":Transitions.EASE_OUT_BOUNCE
						,"onComplete":levelUpOver});
		}
		
		private function levelUpOver():void{
			Starling.juggler.tween(levelUp,1,{"y":Common.H,"transition":Transitions.EASE_IN_OUT_BACK,"delay":3
				,"onComplete":levelUp.removeFromParent});
		}
		
		public function stop():void{
			hitProgress.removeFromParent();
			Starling.juggler.remove(starIcon);
			TexturePool.recycle(MapCellType.STAR,starIcon);
			stage.removeEventListener(EventType.EAT_STAR,eatSatrHandler);
			stage.removeEventListener(EventType.LEVEL_UP,leveUpHandler);
			stage.removeEventListener(EventType.GAME_STATE_CHANGE,onGameStageChangeHandler);
		}
	}
}